package com.lz.androidlibraries.work;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.work.Worker;
import androidx.work.WorkerParameters;

import com.liuyk.commonlib.utility.FileUtils;

import java.io.File;

/**
 * DESC
 * <p>
 * Created by liuyakui on 2022/1/19.
 */
public class UploadLogWorker extends Worker {

    public UploadLogWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
        super(context, workerParams);
    }

    @NonNull
    @Override
    public Result doWork() {
        Log.e("UploadLogWorker", "thread: " + Thread.currentThread().getName());
        //执行成功返回Result.success()
        //执行失败返回Result.failure()
        //需要重新执行返回Result.retry()
        for (int i = 0; i < 20; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            Log.e("UploadLogWorker", i + "doWork");
            File file = new File(Environment.getExternalStorageDirectory(), "WorkManager.txt");
            FileUtils.saveContentFile(file.getAbsolutePath(), i + "doWork ");
        }
        return Result.success();
    }
}
